package com.kang.until;

import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.kang.pojo.PurchaseOrder;
import com.kang.pojo.PurchaseOrderItem;
import com.lowagie.text.BadElementException;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;

/**
 * 采购订单PDF
 * @author zs
 *
 */
public class PurchaseOrderPDF {
	/**
	 * 字体
	 */
	public static BaseFont bf = null;
	/**
	 * 初始化字体
	 */
	static {
		try {
			bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 创建表格统一单元格样式
	 * 
	 * @param str
	 * @return
	 */
	public static Cell createTableCell(String str, Font font) {
		Cell cell = null;
		try {
			cell = new Cell(new Phrase(str, font));
			cell.setBorderColor(new Color(0, 0, 0));
			// 单元格水平对齐方式
			cell.setHorizontalAlignment(Element.ALIGN_CENTER);
			// 单元格垂直对齐方式
			cell.setVerticalAlignment(Element.ALIGN_CENTER);
		} catch (BadElementException e) {
			e.printStackTrace();
		}
		return cell;
	}

	/**
	 * 创建表格并生成表头
	 * 
	 * @param title
	 * @param titleFont
	 * @param tableHeader
	 * @param headerFont
	 * @return
	 * @throws Exception
	 */
	public static Table createTable(String title, Font titleFont, String[] tableHeader, Font headerFont)
			throws Exception {
		Table table = new Table(tableHeader.length);
		table.setBorderWidth(1);
		table.setBorderColor(new Color(0, 0, 0));
		table.setPadding(1);
		table.setSpacing(0);
		table.setWidth(100);
		// 表头说明
		Cell cell = createTableCell(title, titleFont);
		cell.setHeader(true);
		cell.setColspan(tableHeader.length);
		table.addCell(cell);
		// 表头
		for (int i = 0; i < tableHeader.length; i++) {
			cell = createTableCell(tableHeader[i], headerFont);
			table.addCell(cell);
		}
		// 添加此代码后超出每页都会显示表头
		table.endHeaders();
		return table;
	}

	/**
	 * 设置表格数据
	 * 
	 * @param table
	 * @param columns
	 * @param values
	 * @param baseFont
	 * @throws Exception
	 */
	public static void setTableValue(Table table, String[] columns, List<?> values, Font baseFont) throws Exception {
		// 表格
		int length = table.getColumns();
		if (values != null) {
			for (int i = 0; i < values.size(); i++) {
				Object obj = values.get(i);
				for (int j = 0; j < length; j++) {
					if (columns.length <= j) {
						table.addCell(createTableCell("", baseFont));
						continue;
					}
					String field = columns[j];
					String value = "";
					if (obj instanceof Map) {
						value = ((Map<?, ?>) obj).get(field).toString();
					} else {
						String methodName = "get" + field.substring(0, 1).toUpperCase() + field.substring(1);
						Method getterMethod = obj.getClass().getMethod(methodName);
						Object valueObj = getterMethod.invoke(obj);
						if (valueObj != null)
							value = valueObj.toString();
					}
					table.addCell(createTableCell(value, baseFont));
				}
			}
		}
	}
	
	/**
	 * 设置单元格格式
	 * @param cell 单元格
	 * @return
	 */
	public static Cell setInfoCellStyle(Cell cell) {
		cell.setBorderColor(new Color(0, 0, 0));
		// 单元格水平对齐方式
		cell.setHorizontalAlignment(Element.ALIGN_LEFT);
		// 单元格垂直对齐方式
		cell.setVerticalAlignment(Element.ALIGN_CENTER);
		return cell;
	}

	/**
	 * 生成采购订单PDF文件
	 * @param request
	 * @param items
	 * @return
	 */
	public static File buildPurchaseOrderPDF(PurchaseOrder Order,List<PurchaseOrderItem> items) {
		System.out.println("=============采购订单详情==================");
		//PurchaseOrder Order = new PurchaseOrder();
		File pdfFile = new File(FileUtil.TEMP_PATH + "采购订单" + Order.getOrderNumber() + ".pdf");
		try {
			Font baseFont = new Font(bf, 14, Font.NORMAL);
			Document document = new Document(PageSize.A4, 10, 10, 10, 10);// 分别为文档的大小、左、右、上、下的页边距
			PdfWriter.getInstance(document, new FileOutputStream(pdfFile.getAbsolutePath()));
			document.open();
			Paragraph title = new Paragraph("采购订单详情", new Font(bf, 16, Font.BOLD));
			title.setAlignment(1);// 居中对齐
			document.add(title);
			Table table = new Table(6);
			table.setBorderWidth(0);
			table.setBorderColor(new Color(0, 0, 0));
			table.setPadding(1);
			table.setSpacing(0);
			table.setWidth(100);

			Cell cell = new Cell(new Phrase("订单号：", baseFont));
			setInfoCellStyle(cell);
			table.addCell(cell);
			cell = new Cell(new Phrase(Order.getOrderNumber(), baseFont));
			cell.setColspan(3);
			setInfoCellStyle(cell);
			table.addCell(cell);

			cell = new Cell(new Phrase("采购人：", baseFont));
			setInfoCellStyle(cell);
			table.addCell(cell);
			cell = new Cell(new Phrase(Order.getSubmitterName(), baseFont));
			setInfoCellStyle(cell);
			table.addCell(cell);

			cell = new Cell(new Phrase("类型：", baseFont));
			setInfoCellStyle(cell);
			table.addCell(cell);
			cell = new Cell(new Phrase(Order.getMaterialMaterial(), baseFont));
			setInfoCellStyle(cell);
			table.addCell(cell);

//			cell = new Cell(new Phrase("应付：", baseFont));
//			setInfoCellStyle(cell);
//			table.addCell(cell);
//			//Cell中不能放double类型，要强转
//			cell = new Cell(new Phrase(Double.toString(Order.getShouldPay()), baseFont));
//			setInfoCellStyle(cell);
//			table.addCell(cell);
//			
//			cell = new Cell(new Phrase("实付：", baseFont));
//			setInfoCellStyle(cell);
//			table.addCell(cell);
//			cell = new Cell(new Phrase(Double.toString(Order.getActualPay()), baseFont));
//			setInfoCellStyle(cell);
//			table.addCell(cell);
			
			cell = new Cell(new Phrase("供应商：", baseFont));
			setInfoCellStyle(cell);
			table.addCell(cell);
			cell = new Cell(new Phrase(Order.getSupplierName(), baseFont));
			setInfoCellStyle(cell);
			table.addCell(cell);

			cell = new Cell(new Phrase("仓库：", baseFont));
			setInfoCellStyle(cell);
			table.addCell(cell);
			cell = new Cell(new Phrase(Order.getWarehouseName(), baseFont));
			setInfoCellStyle(cell);
			table.addCell(cell);

			cell = new Cell(new Phrase("备注：", baseFont));
			setInfoCellStyle(cell);
			table.addCell(cell);
			cell = new Cell(new Phrase(Order.getSpeaking(), baseFont));
			cell.setColspan(5);
			setInfoCellStyle(cell);
			table.addCell(cell);

			document.add(table);
			String[] tableHeader = { "物品编号", "物品条码", "物品名称", "品牌", "采购小计", "采购单位" };
			table = createTable("采购订单物品列表", baseFont, tableHeader, baseFont);
			String[] columns = { "itemCode", "articleBarCode", "chineseName", "brand", "number", "salesUnit" };
			setTableValue(table, columns, items, baseFont);
			document.add(table);
			SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD hh:mm:ss");//设置日期格式
			String date2 = sdf.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳
			document.add(new Paragraph(" ", baseFont));
			document.add(new Paragraph("打印时间："+date2, baseFont));
			document.add(new Paragraph(" ", baseFont));
			document.add(new Paragraph("负责人签名：", baseFont));
			document.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return pdfFile;
	}
}
